// 可迭代对象
const iterable = {
    names: [111, 222, 333],
    [Symbol.iterator]: function () {
        let index = 0
        return {
            next: () => {
                if (index < this.names.length) {
                    return { done: false, value: this.names[index++] }
                } else {
                    return { done: true, value: undefined }
                }
            }
        }
    }
}
// 可迭代对象的展开运算
const newNames = [...iterable]
const [a, b, c] = iterable
console.log(newNames);
console.log(a, b, c);

for (const item of iterable) {
    console.log(item);
}
// 自定义可迭代对象
class Persons {
    constructor(person) {
        this.persons = person
    }
    [Symbol.iterator]() {
        let index = 0
        return {
            next: () => {
                if (index < this.persons.length) {
                    return { done: false, value: this.persons[index++].name }
                } else {
                    return { done: true, value: undefined }
                }
            },
            return: () => {
                console.log('迭代器提前终止了');
                return { done: true, value: undefined }
            }
        }
    }

}
const p1 = new Persons([{
    name: '张三',
    age: 111
}, {
    name: '李四',
    age: 222
}, {
    name: '王五',
    age: 333
}])
for (const item of p1) {
    console.log(item);
    if (item === '李四') break
}
